package com.atguigu.spzx.manager.scheduled;

import com.atguigu.spzx.manager.mapper.OrderInfoMapper;
import com.atguigu.spzx.manager.mapper.OrderStatisticsMapper;
import com.atguigu.spzx.model.entity.order.OrderStatistics;
import jakarta.annotation.Resource;
import org.joda.time.DateTime;
import org.springframework.stereotype.Component;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;

/**
 * ClassName: OrderStatisticsTask
 * Package: com.atguigu.spzx.manager.scheduled
 * Description:
 *      统计订单数据的定时任务
 * @Author 刘强
 * @Create 2024/7/7 16:17
 * @Version 1.0
 */
@Component
public class OrderStatisticsTask {

    @Resource
    private OrderInfoMapper orderInfoMapper;
    @Resource
    private OrderStatisticsMapper orderStatisticsMapper;

    @Scheduled(cron = "0 0 2 * * ?")
//    @Scheduled(cron = "0/5 * * * * ?")
    public void autoSaveOrderStatistics(){
        // 1. 查询昨日的订单统计数据 order_info
        Date date = new DateTime().minusDays(1).toDate();
        OrderStatistics orderStatistics = orderInfoMapper.getOrderStatisticsByDate(date);
        // 2. 保存到order_statistics表中
        orderStatistics.setCreateTime(new Date());
        orderStatistics.setUpdateTime(new Date());
        orderStatisticsMapper.save(orderStatistics);
    }

}
